#ifndef __USER_MANAGER_H__
#define __USER_MANAGER_H__

#pragma once

#include <SolarHashTable.h>

#include <hash_map>
#include <string>

class User;
class UserSession;
class UserFactory;
class UserManager
{
	typedef stdext::hash_map<DWORD, UserSession*>			USER_AUTH_IDS;
	typedef stdext::hash_map<std::string, UserSession *>	M_USER_LIST;
	typedef std::pair<std::string, UserSession *>			M_USER_LIST_PAIR;

public:
	UserManager();
	~UserManager();

	VOID								InitUserSize( DWORD dwUserSize );
	VOID								ReleaseUser( UserFactory * pRestorePool );

	UserSession *						GetUser( DWORD dwUserKey );
	UserSession *						GetUserByAuthID( DWORD dwAuthID );
	VOID								AddUser( UserSession * pUserSession );
	VOID								RemoveUser( DWORD dwUserKey );

	//VOID								RemoveAllUsersOf( DWORD dwServerSessionIndex );

	inline VOID							SetFirst() { m_pUserHashTable->SetFirst();	}
	inline UserSession *				GetNext() { return m_pUserHashTable->GetNext();	}

	inline DWORD						GetNumberOfUsers() { return m_pUserHashTable->GetDataNum();	}

	
	template <class ObjectOperator>
	VOID								Foreach( ObjectOperator  & op )
	{
		UserSession * pUser = NULL;
		m_pUserHashTable->SetFirst();
		while( ( pUser = m_pUserHashTable->GetNext() ) != NULL )
		{
			op( pUser );
		}
	}

	VOID								AddUserForCharName( User * pUserSession );
	VOID								RemoveUserForCharName( User * pUser );
	UserSession *						FindUserForCharName( TCHAR * pszName );
private:
	util::SolarHashTable<UserSession *> *		m_pUserHashTable;
	USER_AUTH_IDS								m_UserAuthIDs;
	M_USER_LIST									m_CharNameHashTable;
};

extern UserManager g_UserManager;

#endif // __USER_MANAGER_H__